package com.future.hedongjun.home.utils;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.dl7.player.media.IjkPlayerView;
import com.dl7.player.widgets.ShareDialog;
import com.future.baselib.entity.BaseResponse;
import com.future.baselib.http.HttpRequest;
import com.future.hedongjun.R;
import com.future.hedongjun.common.MyApp;
import com.future.hedongjun.home.bean.DanMuBean;
import com.future.hedongjun.home.bean.response.DanmuResponse;

import java.util.List;

import static com.mob.MobSDK.getContext;

/**
 * Date：2018/11/22 14:15
 * author: liu.p.f
 * Describe：
 */
public class DanMaKuSendDialog extends Dialog implements DialogInterface.OnShowListener, DialogInterface.OnDismissListener, DialogInterface.OnCancelListener {
    private static final String TAG = DanMaKuSendDialog.class.getSimpleName();
    private ShareDialog.OnDialogClickListener mOnclick;
    private IjkPlayerView mIjkVideo;
    private String mId;

    /**
     * @param context   上下文
     * @param theme     主题样式
     * @param width     Dilaog的宽度
     * @param height    Dialog的高度
     * @param resLayout Dialog 的布局layout资源id
     * @param gravity   Dialog在Window中的方向位置
     * @param animation Dialog展示和隐藏时候的动画效果
     */
    private DanMaKuSendDialog(Context context, int theme, int width, int height, int resLayout, int gravity, int animation) {
        super(context, theme);
        Window window = getWindow();
        if (window != null) {
            //设置布局
            setContentView(resLayout);
            //设置Dialog的其他属性
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = width;//赋值宽度
            lp.height = height;//赋值高度
            lp.gravity = gravity;//赋值方位
            window.setAttributes(lp);
            //设置Dialog的动画效果
            window.setWindowAnimations(animation);
        }
    }

    private InputMethodManager imm;
    private EditText mDialogEditText;
    private TextView mDialogTextView;
    private TextView mDialogCancel;

    public DanMaKuSendDialog(Context context) {
        this(context, R.style.dialog_style_send_danmu, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, R.layout.activity_custom_danmu, Gravity.BOTTOM, 0);
        //初始化软键盘服务管理类
        imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        //设置点击Dialog外部会消失 ，下面两个设置效果会回调onCancel --onDismiss方法，我们在里面关闭一下软键盘
        setCanceledOnTouchOutside(true);
        //点返回键Diloag会消失
        setCancelable(true);
        //初始化布局UI
        mDialogTextView = (TextView) findViewById(R.id.tv_send);
        mDialogEditText = (EditText) findViewById(R.id.et_danMu);
        mDialogCancel = findViewById(R.id.tv_cancel);
        // 使软键盘不全屏显示，只占用一部分屏幕,对应xml标签 android:imeOptions="flagNoExtractUi
        mDialogEditText.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
        //初始化Dialog的监听
        setOnShowListener(this);
        setOnDismissListener(this);
        setOnCancelListener(this);

        mDialogCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (imm != null) {
                    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });

        mDialogTextView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new HttpRequest<DanmuResponse>()
                        .with(getContext())
                        .setApiCode("_pushcontent_001")
                        .addParam("id", mId)
                        .addParam("userToken", MyApp.getInstance().getUserToken())
                        .addParam("user_cotent", mDialogEditText.getText().toString())
                        .addParam("teacher_id", mIjkVideo.getCurPosition())
                        .addSuccessListener(new HttpRequest.OnNetworkSuccess() {
                            @Override
                            public void onSuccess(BaseResponse response) {

                            }
                        }).start(new DanmuResponse());
                mIjkVideo.sendDanmaku(mDialogEditText.getText().toString(), false);
                mDialogEditText.setText("");
                dismiss();
                if (imm != null) {
                    imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        });
    }

    //Dialog出现的时候监听 ,Dialog展示的时候就弹出软键盘
    @Override
    public void onShow(DialogInterface dialog) {
        Log.e(TAG, "----Dialog----onShow-----------");
        if (mDialogEditText != null) {
            mDialogEditText.post(new Runnable() {
                @Override
                public void run() {
                    if (imm != null) {
                        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
                    }
                    try {
                        Thread.sleep(60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }

    //Dialog消失的时候监听
    @Override
    public void onDismiss(DialogInterface dialog) {
        Log.e(TAG, "----Dialog----onDismiss-----------");
    }

    //取消Dialog的监听 取消的时候关闭软键盘
    @Override
    public void onCancel(DialogInterface dialog) {
        Log.e(TAG, "----Dialog----onCancel-----------");
        if (mDialogEditText != null) {
            mDialogEditText.post(new Runnable() {
                @Override
                public void run() {
                    if (imm != null) {
                        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_NOT_ALWAYS);
                    }
                    try {
                        Thread.sleep(60);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }


    public void setIjkVideo(IjkPlayerView ijkDetailPlayer, String id) {
        this.mIjkVideo = ijkDetailPlayer;
        this.mId = id;
    }
}
